package com.ttc.mBackup.BackupSMS;

import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.util.Log;


public class RestoreSMS extends SMSFactory {

	private static final String IOEXCEPTION = "IOException";
	private static final String JSONEXCEPTION = "JsonException";
	
	private int restoreModeCode;
	
	/**
	 * 
	 * @param _sms
	 * @param phoneContact
	 * @param filename
	 * @param locationCode
	 * @param restoreModeCode
	 */
	
	public RestoreSMS(SMSStatistics _sms, ArrayList<String> phoneContact,
			String filename, int locationCode, int restoreModeCode) {
		super(_sms, phoneContact, filename, locationCode);
		this.restoreModeCode = restoreModeCode;
	}
	
	/**
	 * 
	 * @return
	 */
	public boolean RestoreMessage(){
		String jsonString = "";
		JSONObject jsonObject = null;
		try {
			jsonString = new FileBrowser(filename).ReadBackupFile();
		} catch (IOException e) {
			Log.e(IOEXCEPTION, "Can't read the file" + e.getMessage());
			return false;
		}
		
		if (restoreModeCode == DialogCode.CODE_PROGRESS_RESTORE_OVERRIDE) {
			//delete all message
			sms.deleteAllMessage();
		}
		try {
			jsonObject = new JSONObject(jsonString);
		} catch (JSONException e) {
			Log.e(JSONEXCEPTION, e.getMessage());
			return false;
		}
		int sumOfSMS = 0;
		try {
			//get sum of message in the backup file
			sumOfSMS = jsonObject.getInt("smses");
		} catch (JSONException e) {
			Log.e(JSONEXCEPTION, e.getMessage());
			return false;
		}
		if(phoneContact.get(0).equals("Check all")){
			for (int i = 0; i < sumOfSMS; i++) {
				insertMessage(jsonObject, i);
			}
		}else if(!phoneContact.get(0).equals("Check all")){
			for (int i = 0; i < sumOfSMS; i++) {
				for (int j = 0; j < phoneContact.size(); j++) {
					JSONArray jsonArray = null;
					try {
						jsonArray = jsonObject.getJSONArray("sms");
					} catch (JSONException e) {
						Log.e(JSONEXCEPTION, e.getMessage());
					}
					String address = "";
					try {
						address = jsonArray.getJSONObject(i).getString("address");
					} catch (JSONException e) {
						Log.e(JSONEXCEPTION, e.getMessage());
					}
					if(address.equals(phoneContact.get(j))){
						insertMessage(jsonObject, i);
					}
				}
			}
		}	
		return true;
	}
	
	/**
	 * 
	 * @param jsonObject
	 * @param i
	 */
	private void insertMessage(JSONObject jsonObject, int i){
		Long date = null;
		String body = "", address ="";
		int read = 0, locked = 0, status = 0, type = 0;
		
		JSONArray jsonArray = null;
		try {
			jsonArray = jsonObject.getJSONArray("sms");
		} catch (JSONException e) {
			Log.e(JSONEXCEPTION, e.getMessage());
		}
		try {
			address = jsonArray.getJSONObject(i).getString("address");
		} catch (JSONException e) {
			Log.e(JSONEXCEPTION, e.getMessage());
		}
		try {
			body = jsonArray.getJSONObject(i).getString("body");
		} catch (JSONException e) {
			Log.e(JSONEXCEPTION, e.getMessage());
		}
		try {
			status = jsonArray.getJSONObject(i).getInt("status");
		} catch (JSONException e) {
			Log.e(JSONEXCEPTION, e.getMessage());
		}
		try {
			read = jsonArray.getJSONObject(i).getInt("read");
		} catch (JSONException e) {
			Log.e(JSONEXCEPTION, e.getMessage());
		}
		try {
			locked = jsonArray.getJSONObject(i).getInt("locked");
		} catch (JSONException e) {
			Log.e(JSONEXCEPTION, e.getMessage());
		}
		try {
			type = jsonArray.getJSONObject(i).getInt("type");
		} catch (JSONException e) {
			Log.e(JSONEXCEPTION, e.getMessage());
		}
		try {
			date = jsonArray.getJSONObject(i).getLong("date");
		} catch (JSONException e) {
			Log.e(JSONEXCEPTION, e.getMessage());
		}
		Log.v(CHECK, "Read Backup Data Successfully");
		
		ContentValues values = new ContentValues();
		values.put("address", address);
		values.put("body", body);
		values.put("date", date);
		values.put("read", read);
		values.put("locked", locked);
		values.put("type", type);
		values.put("status", status);
		sms.insertMessage(values);
		Log.v(CHECK, "Restore Message OK");
	}
}
